package lg.minecraft.plugin.pokemonmenu.gui.edit;

import com.cobblemon.mod.common.api.moves.Move;
import com.cobblemon.mod.common.api.moves.MoveSet;
import com.cobblemon.mod.common.api.moves.MoveTemplate;
import com.cobblemon.mod.common.pokemon.Pokemon;
import lg.minecraft.plugin.cobblemoncore.util.PokemonDataCache;
import lg.minecraft.plugin.library.inventory.CancelInventoryContext;
import lg.minecraft.plugin.library.inventory.ClickInventoryContext;
import lg.minecraft.plugin.library.inventory.InventoryContext;
import lg.minecraft.plugin.library.inventory.LGInventory;
import lg.minecraft.plugin.pokemonmenu.LGPokemonMenu;
import lg.minecraft.plugin.pokemonmenu.command.InputType;
import lg.minecraft.plugin.pokemonmenu.gui.EditInventor;
import lg.minecraft.plugin.pokemonmenu.handler.input.PlayerInputHandler;
import lg.minecraft.plugin.pokemonmenu.handler.input.PlayerInputManager;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

/**
 * @author langle__
 * @version 1.0
 */
public class EditMoveInventor extends LGInventory implements PlayerInputHandler {

    private static final int[] GLASS_ARRAY = {45, 46, 47, 48, 50, 51, 52, 53};
    private static final int[] MOVE_INDEX = {19, 21, 23, 25};

    private final Pokemon pokemon;
    private int waitInputIndex;

    public EditMoveInventor(Pokemon pokemon) {
        super(6, LGPokemonMenu.getMessage().getGuiTitleEditMove());
        this.pokemon = pokemon;
    }

    @Override
    public void onInventoryOpen(CancelInventoryContext context) {
        MoveSet moveSet = pokemon.getMoveSet();
        ItemStack editMoveNoExist = LGPokemonMenu.getGuiItem().getEditMoveNoExist();
        for (int i = 0; i < 4; i++) {
            Move move = moveSet.get(i);
            int slot = MOVE_INDEX[i];
            inventory.setItem(slot, move == null ? editMoveNoExist : LGPokemonMenu.getGuiItem().getEditMoveExist(move));
        }

        for (int slot : GLASS_ARRAY) inventory.setItem(slot, LGPokemonMenu.getGuiItem().getBorder());

        inventory.setItem(49, LGPokemonMenu.getGuiItem().getBack());
    }

    @Override
    public void onInventoryClick(ClickInventoryContext context) {
        context.setCancelled(true);
        Player player = context.getPlayer();
        int slot = context.getSlot();

        int moveIndexBySlot = getMoveIndexBySlot(slot);
        if (moveIndexBySlot >= 0) {
            player.sendMessage(LGPokemonMenu.getMessage().getInputMove());
            waitInputIndex = moveIndexBySlot;
            player.closeInventory();
            PlayerInputManager.waitForInput(player, InputType.POKEMON_MOVE, this);
            player.playSound(player.getLocation(), Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 1.0F, 1.8F);
        } else if (slot == 49) {
            new EditInventor(pokemon).openInventory(player);
            player.playSound(player.getLocation(), Sound.UI_BUTTON_CLICK, 0.5F, 1F);
        }
    }

    @Override
    public void onInventoryClose(InventoryContext context) {
    }

    @Override
    public void onPlayerInput(Player player, String input) {
        MoveTemplate move = PokemonDataCache.getInstance().getMoveByKey(input);
        if (move == null) {
            player.sendMessage(LGPokemonMenu.getMessage().getNoMove());
        } else {
            pokemon.getMoveSet().setMove(waitInputIndex, move.create());
        }
        openInventory(player);
    }

    private int getMoveIndexBySlot(int slot) {
        for (int i = 0; i < MOVE_INDEX.length; i++) {
            if (MOVE_INDEX[i] == slot) {
                return i;
            }
        }
        return -1;
    }

}

